package com.courir.reporter.birt;

import java.awt.Composite;
import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;

import lombok.val;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;

public class View extends ViewPart {
	public static final String ID = "de.fcstraberg.vlreport.view";

	// private TableViewer viewer;

	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			if (parent instanceof Object[]) {
				return (Object[]) parent;
			}
			return new Object[0];
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages()
					.getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		/*
		 * viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL |
		 * SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider());
		 * viewer.setLabelProvider(new ViewLabelProvider()); // Provide the
		 * input to the ContentProvider viewer.setInput(new String[] {"One",
		 * "Two", "Three"});
		 */
		String path = "";
		try {
			// val bundle = Platform.getBundle("de.fcstraberg.vlreport");
			val bundle = Activator.getDefault().getBundle();
			val url = FileLocator.find(bundle, new Path(
					"report/ergebnisliste.rptdesign"), null);
			path = FileLocator.toFileURL(url).getPath();
		} catch (MalformedURLException me) {
			System.out.println("Fehler bei URL " + me.getStackTrace());
		} catch (IOException e) {
			e.printStackTrace();
		}

		val browser = new Browser(parent, SWT.NONE);
		// Use the filename of your report
		WebViewer.display(path, WebViewer.HTML, browser, "frameset");
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		// viewer.getControl().setFocus();
	}
}